from google.appengine.ext import db

from gaeo.controller import BaseController

from model.config import Config

class ConfigController(BaseController):
    def create(self):
        r = Config(
            k = self.params.get('k', None),
            v = self.params.get('v', None),
        )
        r.put()
        self.redirect('/config')

    def destroy(self):
        r = Config.get(self.params.get('key'))
        if r is not None:
            r.delete()
        self.redirect('/config')

    def edit(self):
        rec = Config.get(self.params.get('key'))
        if rec:
            setattr(self, 'k', rec.k)
            setattr(self, 'v', rec.v)
        else:
            setattr(self, 'k', 'Error')
            setattr(self, 'v', 'Error')

    def index(self):
        query = Config.all()
        self.result = query

    def new(self):
        pass

    def show(self):
        rec = Config.get(self.params.get('key'))
        if rec:
            setattr(self, 'k', rec.k)
            setattr(self, 'v', rec.v)
        else:
            setattr(self, 'k', 'Error')
            setattr(self, 'v', 'Error')

    def update(self):
        r = Config.get(self.params.get('key'))
        r.k = self.params.get('k', None)
        r.v = self.params.get('v', None)
        r.put()
        self.redirect('/config')
